/*
 *    Copyright 2009-2012 The MyBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.hama.iotrust.common.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;

import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;

import com.hama.iotrust.common.mybatis.DialectalSqlSessionFactory;
import com.hama.iotrust.common.mybatis.dialect.Dialect;

/*
 * Builds {@link SqlSession} instances.
 *
 */
public class DialectalSqlSessionFactoryBuilder {

    public SqlSessionFactory build(Reader reader, Dialect dialect) {
        return build(reader, null, null, dialect);
    }

    public SqlSessionFactory build(Reader reader, String environment, Dialect dialect) {
        return build(reader, environment, null, dialect);
    }

    public SqlSessionFactory build(Reader reader, Properties properties, Dialect dialect) {
        return build(reader, null, properties, dialect);
    }

    public SqlSessionFactory build(Reader reader, String environment, Properties properties, Dialect dialect) {
        try {
            XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
            return build(parser.parse(), dialect);
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", e);
        } finally {
            ErrorContext.instance().reset();
            try {
                reader.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    public SqlSessionFactory build(InputStream inputStream, Dialect dialect) {
        return build(inputStream, null, null, dialect);
    }

    public SqlSessionFactory build(InputStream inputStream, String environment, Dialect dialect) {
        return build(inputStream, environment, null, dialect);
    }

    public SqlSessionFactory build(InputStream inputStream, Properties properties, Dialect dialect) {
        return build(inputStream, null, properties, dialect);
    }

    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties, Dialect dialect) {
        try {
            XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
            return build(parser.parse(), dialect);
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", e);
        } finally {
            ErrorContext.instance().reset();
            try {
                inputStream.close();
            } catch (IOException e) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }

    public SqlSessionFactory build(Configuration config, Dialect dialect) {
        return new DialectalSqlSessionFactory(config, dialect);
    }

}
